/*
	YoghurtGum - 2D Game Engine

	Copyright 2009-2011 Quinten Lansu
	
	Licensed under the Apache License, Version 2.0 (the "License"); you may not 
	use this file except in compliance with the License. You may obtain a copy 
	of the License at 
		
		http://www.apache.org/licenses/LICENSE-2.0 
		
	Unless required by applicable law or agreed to in writing, software 
	distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 
	WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 
	License for the specific language governing permissions and limitations 
	under the License. 
*/

#ifndef _YGLOGSTREAM_H_
#define _YGLOGSTREAM_H_

#include "YGSources.h"
#include "YGFunctions.h"

namespace YoghurtGum
{

	class LogStream
	{

		struct Message
		{
			void SetString(char* a_String, int32 a_Length = -1);

			char* msg;
			uint32 length;
		};

	public:

		enum Level
		{
			LEVEL_INFO,
			LEVEL_TRACE,
			LEVEL_ERROR,
			LEVEL_FATAL,
			LEVEL_ALL
		};

		LogStream(char* a_Format);
		~LogStream();

		/*
			Parses everything Date::Format parses.

			Specific flags:
			$lel    = Error level
			$lEL    = Error level uppercase
			$lmsg   = The message
			
			Standard:
			($TIME) - $EL - $msg
		*/
		void SetFormat(char* a_Format);

		virtual void Output(char* a_String, unsigned int a_Length) { }

	protected:

		virtual void Format(char* a_Message, Level a_Level);

		virtual void AddErrorLevel(Level a_Level, char* a_Output, Message* a_String);
		virtual void AddErrorLevelCap(Level a_Level, char* a_Output, Message* a_String);
		virtual void AddDate(Level a_Level, char* a_Output, Message* a_String);
		virtual void AddString(Level a_Level, char* a_Output, Message* a_String);
		virtual void AddMessage(Level a_Level, char* a_Output, Message* a_String);

		static void WrapAddErrorLevel(void* a_Target, Level a_Level, char* a_Output, Message* a_String);
		static void WrapAddErrorLevelCap(void* a_Target, Level a_Level, char* a_Output, Message* a_String);
		static void WrapAddDate(void* a_Target, Level a_Level, char* a_Output, Message* a_String);
		static void WrapAddString(void* a_Target, Level a_Level, char* a_Output, Message* a_String);
		static void WrapAddMessage(void* a_Target, Level a_Level, char* a_Output, Message* a_String);

		void (*m_Functions[32])(void*, Level, char*, Message*);
		uint32 m_FunctionTotal;

		Message* m_Strings[32];

		char m_LogLevels[5][16];
		char m_Formatted[256];
		char m_Format[5][256];
		bool m_DateTime;
		
	}; // class LogStream

}; // namespace YoghurtGum

#endif
